//
//  TravelView.m
//  ToWhere
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "TravelView.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import <UIButton+WebCache.h>
#import "DataAnalysis.h"
#import <UIImageView+WebCache.h>
#define Travel_TOPIC_URL @"http://chanyouji.com/api/articles.json?page=1"
@interface TravelView ()
//标题
@property (nonatomic, retain) UILabel *titleLabel;

@property (nonatomic, retain) UILabel *label;

//大btn上label
@property (nonatomic, retain) UILabel *bigBtnUpLabel;
//大btn中view
@property (nonatomic, retain) UIView *bigBtnview;
//大btn下label
@property (nonatomic, retain) UILabel *bigBtnDownLabel;

//左btn上label
@property (nonatomic, retain) UILabel *leftBtnUpLabel;

@property (nonatomic, retain) UIView *bigBtnTwoview;

@property (nonatomic, retain) UILabel *bigBtnLabel;

//右btn上label
@property (nonatomic, retain) UILabel *rightBtnUpLabel;


//分割View
@property (nonatomic, retain) UIView *viewF;

@property (nonatomic, retain) NSArray *array;

@property (nonatomic, retain) NSMutableArray *data;

@end

@implementation TravelView

-(void)dealloc
{
    [_array release];
    [_arr release];
    [_titleLabel release];
    [_label release];
    [_bigBtn release];
    [_leftBtn release];
    [_rightBtn release];
    [_viewF release];
    [_moreBtn release];
    
    [_bigBtnUpLabel release];
    [_bigBtnview release];
    [_bigBtnDownLabel release];
    [_bigBtnTwoview release];
    [_bigBtnLabel release];
    
    [_leftBtnUpLabel release];
    
    [_rightBtnUpLabel release];
   
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
     
       self.arr = [NSMutableArray array];
        self.data = [NSMutableArray array];
        [self createView];
        [self gain];
        [self load];
        
    }
    
    
    return self;
   
}
- (void)gain{

    NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSString *Caches = [cachesPath stringByAppendingPathComponent:@"CachesField"];
    NSString *folderPath = [Caches stringByAppendingString:@"SYZT.plist"];
    NSMutableArray *array = [NSMutableArray array];
    array = [NSKeyedUnarchiver unarchiveObjectWithFile:folderPath];
    if (array.count == 0) {
        return;
    }
    for (NSDictionary * dic in array) {
        
        [self.data addObject:dic];
        
    }


    
   [self.bigBtn sd_setBackgroundImageWithURL:[self.data[7] valueForKey:@"image_url"] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"Travel_place"]];
    self.bigBtn.tag = 10000 + (NSInteger)[self.data[7] valueForKey:@"id"];
    
    self.bigBtnUpLabel.text = [self.data[7] valueForKey:@"name"];
    self.bigBtnDownLabel.text = [self.data[7] valueForKey:@"title"];
    
    [self.leftBtn sd_setBackgroundImageWithURL:[self.data[1] valueForKey:@"image_url"] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"Travel_place"]];
    
    self.leftBtn.tag = 10000 + (NSInteger)[self.data[1] valueForKey:@"id"];
    
    
    self.leftBtnUpLabel.text = [self.data[1] valueForKey:@"name"];
    
    [self.rightBtn sd_setBackgroundImageWithURL:[self.data[2] valueForKey:@"image_url"] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"Travel_place"]];
    self.rightBtn.tag = 10000 + (NSInteger)[self.data[2] valueForKey:@"id"];
    self.rightBtnUpLabel.text = [self.data[2] valueForKey:@"name"];
}
-(void)load
{
    [DataAnalysis dataAnalysisGETWithUrlString:Travel_TOPIC_URL parameters:nil showHuD:YES hiddenView:nil successfulBlock:^(id responseObject) {
     
        NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *Caches = [cachesPath stringByAppendingPathComponent:@"CachesField"];
        [fileManager createDirectoryAtPath:Caches withIntermediateDirectories:YES attributes:nil error:nil];
        NSString *folderPath = [Caches stringByAppendingString:@"SYZT.plist"];
        [NSKeyedArchiver archiveRootObject:responseObject toFile:folderPath];
        
        self.array = responseObject;
        if (self.array.count == 0) {
            
            return;
        }
        for (NSDictionary * dic in self.array) {
            
            [self.arr addObject:dic];
        
        }
  
        [self.bigBtn sd_setBackgroundImageWithURL:[self.arr[7] valueForKey:@"image_url"] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"Travel_place"]];
        self.bigBtn.tag = 10000 + (NSInteger)[self.arr[7] valueForKey:@"id"];
     
        self.bigBtnUpLabel.text = [self.arr[7] valueForKey:@"name"];
        self.bigBtnDownLabel.text = [self.arr[7] valueForKey:@"title"];
        
        [self.leftBtn sd_setBackgroundImageWithURL:[self.arr[1] valueForKey:@"image_url"] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"Travel_place"]];

        self.leftBtn.tag = 10000 + (NSInteger)[self.arr[1] valueForKey:@"id"];
       
        
       self.leftBtnUpLabel.text = [self.arr[1] valueForKey:@"name"];

         [self.rightBtn sd_setBackgroundImageWithURL:[self.arr[2] valueForKey:@"image_url"] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"Travel_place"]];
        self.rightBtn.tag = 10000 + (NSInteger)[self.arr[2] valueForKey:@"id"];
        self.rightBtnUpLabel.text = [self.arr[2] valueForKey:@"name"];
       
    } failureBlock:^(NSError *error) {
        NSLog(@"error: %@", error);
        
    }];

}

-(void)createView
{
    self.label = [[UILabel alloc] init];
    self.label.frame = CGRectMake(10 *kWidth, 0, 5 *kWidth, self.frame.size.height / 20);
    self.label.backgroundColor = [UIColor colorWithRed:0.400 green:1.000 blue:0.800 alpha:1.000];
    [self addSubview:self.label];
    [_label release];
    self.titleLabel = [[UILabel alloc]init];
    //self.titleLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 10);
    self.titleLabel.frame = CGRectMake(20 *kWidth, 0, 80 *kWidth, self.frame.size.height / 20);
    //self.titleLabel.backgroundColor = [UIColor colorWithWhite:0.927 alpha:1.000];
    //self.titleLabel.alpha = 0.5;
    self.titleLabel.text = @"专题";
    self.titleLabel.textColor = [UIColor orangeColor];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.font = [UIFont systemFontOfSize:17 *kWidth];
    [self addSubview:self.titleLabel];
    [_titleLabel release];
    
    self.bigBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.bigBtn.frame = CGRectMake(5 * kWidth , self.titleLabel.frame.size.height + 5*kHeight, self.frame.size.width - 10 * kWidth, self.frame.size.height / 2  - 10*kHeight);

    [self addSubview:self.bigBtn];
    
    
    self.bigBtnUpLabel = [[UILabel alloc]init];
    self.bigBtnUpLabel.frame = CGRectMake(0, self.bigBtn.frame.size.height / 2 - 30 * kHeight , self.bigBtn.frame.size.width, 28 * kHeight);
   
    self.bigBtnUpLabel.textAlignment = NSTextAlignmentCenter;
    self.bigBtnUpLabel.font = [UIFont systemFontOfSize:20 *kWidth];
    self.bigBtnUpLabel.textColor = [UIColor whiteColor];
    
    [self.bigBtn addSubview:self.bigBtnUpLabel];
    [_bigBtnUpLabel release];
    
    self.bigBtnview = [[UIView alloc]init];
    self.bigBtnview.frame = CGRectMake( 5 * kWidth, self.bigBtn.frame.size.height / 2 - 1 * kHeight, self.bigBtn.frame.size.width / 2 - 12.5 * kWidth, 1 * kHeight);
    self.bigBtnview.backgroundColor = [UIColor whiteColor];
    
    [self.bigBtn addSubview:self.bigBtnview];
    [_bigBtnview release];
    self.bigBtnTwoview = [[UIView alloc]init];
    self.bigBtnTwoview.frame = CGRectMake(self.bigBtn.frame.size.width / 2 + 7.5 * kWidth, self.bigBtn.frame.size.height / 2 - 1 * kHeight, self.bigBtn.frame.size.width / 2 - 12.5 * kWidth, 1 * kHeight);
    self.bigBtnTwoview.backgroundColor = [UIColor whiteColor];
    
    [self.bigBtn addSubview:self.bigBtnTwoview];
    [_bigBtnTwoview release];
    
    self.bigBtnLabel = [[UILabel alloc]init];
    self.bigBtnLabel.frame = CGRectMake(self.bigBtn.frame.size.width / 2 - 2.5 *kWidth, self.bigBtn.frame.size.height / 2 - 3*kHeight, 5 * kWidth, 5 * kHeight);
    self.bigBtnLabel.layer.masksToBounds = YES;
    self.bigBtnLabel.layer.cornerRadius = 2.5 *kWidth;
    self.bigBtnLabel.backgroundColor = [UIColor whiteColor];
    [self.bigBtn addSubview:self.bigBtnLabel];
    [_bigBtnLabel release];
    
    
    self.bigBtnDownLabel = [[UILabel alloc]init];
    self.bigBtnDownLabel.frame = CGRectMake(0, self.bigBtn.frame.size.height / 2 + 1 * kHeight, self.bigBtn.frame.size.width, 28 * kHeight);
    self.bigBtnDownLabel.textAlignment = NSTextAlignmentCenter;
    self.bigBtnDownLabel.font = [UIFont systemFontOfSize:17 *kWidth];
    self.bigBtnDownLabel.textColor = [UIColor whiteColor];
    [self.bigBtn addSubview:self.bigBtnDownLabel];
    [_bigBtnDownLabel release];
    
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftBtn.frame = CGRectMake(5 * kWidth, self.bigBtn.frame.size.height + self.titleLabel.frame.size.height + 10 * kHeight, self.frame.size.width / 2 - 10 * kWidth, self.frame.size.height / 4 + 10 * kHeight);
    [self addSubview:self.leftBtn];
    
    
    self.leftBtnUpLabel = [[UILabel alloc]init];
    self.leftBtnUpLabel.frame = CGRectMake(20, self.leftBtn.frame.size.height / 2 - 20 * kHeight, self.leftBtn.frame.size.width - 40, 40* kHeight);
    self.leftBtnUpLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
    self.leftBtnUpLabel.layer.masksToBounds = YES;
    self.leftBtnUpLabel.layer.borderWidth = 1;
    self.leftBtnUpLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.leftBtnUpLabel.textAlignment = NSTextAlignmentCenter;
    self.leftBtnUpLabel.font = [UIFont systemFontOfSize:15 * kWidth];
    self.leftBtnUpLabel.textColor = [UIColor whiteColor];
    [self.leftBtn addSubview:self.leftBtnUpLabel];
    [_leftBtnUpLabel release];
    
    
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightBtn.frame = CGRectMake(self.frame.size.width / 2 + 5 * kWidth, self.leftBtn.frame.origin.y, self.leftBtn.frame.size.width, self.frame.size.height / 4 + 10 * kHeight);
    [self addSubview:self.rightBtn];
    
    
    self.rightBtnUpLabel = [[UILabel alloc]init];
    self.rightBtnUpLabel.frame = CGRectMake(20 * kWidth, self.rightBtn.frame.size.height / 2 - 20 * kHeight, self.rightBtn.frame.size.width - 40, 40 *kHeight);
 
    self.rightBtnUpLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
    self.rightBtnUpLabel.layer.masksToBounds = YES;
    self.rightBtnUpLabel.layer.borderWidth = 1;
    self.rightBtnUpLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.rightBtnUpLabel.textAlignment = NSTextAlignmentCenter;
    self.rightBtnUpLabel.font = [UIFont systemFontOfSize:15 * kWidth];
    self.rightBtnUpLabel.textColor = [UIColor whiteColor];

    self.rightBtnUpLabel.textColor = [UIColor whiteColor];
    [self.rightBtn addSubview:self.rightBtnUpLabel];
    [_rightBtnUpLabel release];
    
    
    self.viewF = [[UIView alloc]init];
    self.viewF.frame = CGRectMake(0, self.rightBtn.frame.size.height + self.rightBtn.frame.origin.y + 10 * kHeight, self.frame.size.width, 3 *kHeight);
    self.viewF.backgroundColor = [UIColor colorWithRed:0.882 green:0.882 blue:0.902 alpha:1];
    [self addSubview:self.viewF];
    [_viewF release];
    
    self.moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.moreBtn.frame = CGRectMake(0, self.viewF.frame.size.height + self.viewF.frame.origin.y + 8 *kHeight, self.frame.size.width, 30 *kHeight);
    [self.moreBtn setTitle:@"<更多精典专题>" forState:UIControlStateNormal];
    [self.moreBtn setTitleColor:[UIColor colorWithRed:0.644 green:0.643 blue:0.657 alpha:1.000] forState:UIControlStateNormal];
    
    self.moreBtn.titleLabel.font = [UIFont systemFontOfSize:15 *kWidth];
    self.moreBtn.tag = 10000;
    [self addSubview:self.moreBtn];

}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
